package com.mobile.liuyi_s.orderlunch.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.mobile.liuyi_s.orderlunch.MyApplication;
import com.mobile.liuyi_s.orderlunch.R;
import com.mobile.liuyi_s.orderlunch.adapter.OrderListAdapter;
import com.mobile.liuyi_s.orderlunch.entity.Order;

import java.util.ArrayList;
import java.util.List;

public class OrderListActivity extends AppCompatActivity implements OrderListAdapter.onChildClickListener {

    private static final String TAG = "OrderListActivity";
    private MyApplication mApplication;
    private OrderListAdapter mAdapter;
    private RecyclerView mRecyclerView;
    private Toolbar mToolbar;
    private String mSite;
    private List<Order> mOrderList50;
    private List<Order> mOrderList51;
    private List<Order> mOrderList52;
    private List<Order> mOrderList53;
    private List<Order> mOrderList54;

    private int mClickSite = 0;
    private int mClickPosition = 99999;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_list50);
        mApplication = ((MyApplication) getApplication());
        initView();
        RecyclerView.LayoutManager manager =
                new LinearLayoutManager(this, LinearLayout.VERTICAL, false);
        mRecyclerView.setLayoutManager(manager);
        mAdapter = new OrderListAdapter(this, new ArrayList<Order>());
        mAdapter.onChildClickListener(this);

        String site = getIntent().getStringExtra("site");
        switch (site) {
            case "50":
                mOrderList50 = mApplication.getOrderList50();
                if (mOrderList50 != null) {
                    mAdapter.addAll(mOrderList50);
                    mClickSite = 50;
                } else {
                    this.finish();
                }
                break;
            case "51":
                mOrderList51 = mApplication.getOrderList51();
                if (mOrderList51 != null) {
                    mAdapter.addAll(mOrderList51);
                    mClickSite = 51;
                } else {
                    this.finish();
                }
                break;
            case "52":
                mOrderList52 = mApplication.getOrderList52();
                if (mOrderList52 != null) {
                    mAdapter.addAll(mOrderList52);
                    mClickSite = 52;
                } else {
                    this.finish();
                }
                break;
            case "53":
                mOrderList53 = mApplication.getOrderList53();
                if (mOrderList53 != null) {
                    mAdapter.addAll(mOrderList53);
                    mClickSite = 53;
                } else {
                    this.finish();
                }
                break;
            case "54":
                mOrderList54 = mApplication.getOrderList54();
                if (mOrderList54 != null) {
                    mAdapter.addAll(mOrderList54);
                    mClickSite = 54;
                } else {
                    this.finish();
                }
                break;
            default:
                break;
        }
        mRecyclerView.setAdapter(mAdapter);

    }

    private void initView() {
        initToolbar();
        mRecyclerView = ((RecyclerView) findViewById(R.id.recycler_view_order_list));
    }

    private void initToolbar() {
        mToolbar = ((Toolbar) findViewById(R.id.toolbar_order_list));
        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);
    }

    @Override
    public void onChildClickListener(RecyclerView parent, View view, int position) {
        mClickPosition = position;
        String clickSite = null;
        if (mClickPosition != 99999) {
            switch (mClickSite) {
                case 50:
                    clickSite = "50";
                    break;
                case 51:
                    clickSite = "51";
                    break;
                case 52:
                    clickSite = "52";
                    break;
                case 53:
                    clickSite = "53";
                    break;
                case 54:
                    clickSite = "54";
                    break;
                default:
                    break;
            }
            if (clickSite != null) {
                Intent intent = new Intent(this, OrderDetailActivity.class);
                intent.putExtra("site",clickSite);
                intent.putExtra("position", position);
                startActivity(intent);
            } else {
                Log.d(TAG, "onChildClickListener: 获取站点号失败");
            }
        } else {
            Log.d(TAG, "onChildClickListener: 获取itemView位置失败");
        }

    }

}
